import { FieldParam, FieldTupleParam } from "../main.ts";
import { Handler } from "./index.ts";

export class ZeroValueHandler implements Handler {
  check<T, Extra>(param: FieldParam<T, Extra>): void {
    if (param.value !== undefined) {
      throw new Error(`${param.op}'s value must be undefined`);
    }
  }

  process<T, Extra>(param: FieldParam<T, Extra>, group?: string): string {
    let str = "";
    if (group) {
      str = `${group}.${String(param.label)}-op=${param.op}`;
    } else {
      str = `${String(param.label)}-op=${param.op}`;
    }
    return str;
  }

  processTuple<T, Extra>(
    param: FieldTupleParam<T, Extra>,
    group?: string,
  ): string {
    let str = "";
    if (group) {
      str = `${group}.${String(param[0])}-op=${param[1]}`;
    } else {
      str = `${String(param[0])}-op=${param[1]}`;
    }
    return str;
  }

  checkTuple<T, Extra>(param: FieldTupleParam<T, Extra>): void {
    if (param[2] !== undefined) {
      throw new Error(`${param[1]}'s value must be undefined`);
    }
  }

  processObj<T, Extra>(
    obj: object,
    param: FieldParam<T, Extra>,
    group?: string,
  ): void {
    if (group) {
      Object.assign(obj, { [`${group}.${param.label}-op`]: param.op });
    } else {
      Object.assign(obj, { [`${param.label}-op`]: param.op });
    }
  }

  processObjTuple<T, Extra>(
    obj: object,
    param: FieldTupleParam<T, Extra>,
    group?: string,
  ): void {
    if (group) {
      Object.assign(obj, { [`${group}.${param[0]}-op`]: param[1] });
    } else {
      Object.assign(obj, { [`${param[0]}-op`]: param[1] });
    }
  }
}
